/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * <p>Project: javase - Img8
 * <p>Powered by webrx On 2021-12-21 09:08:21
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Img8 {
    public static void main(String[] args) throws IOException {
        var src = ImageIO.read(new File("c:/s.jpg"));
        var sw = src.getWidth();
        var sh = src.getHeight();

        var dw = 120;
        var dh = 120;
        var dst = new BufferedImage(dw, dh, 1);
        var g = dst.createGraphics();
        //缩略图 0,0 dw,dh 目标宽高
        //g.drawImage(src, 0, 0, dw, dh, null);
        //g.drawImage(src, 0, 0, dw, dh, 0, 0, sw, sh, null);

        //剪切图像 0,0 sw,sh 源图像宽高
        int x = 130;
        int y = 10;
        //g.drawImage(src, -x, -y, sw, sh, null);
        //裁剪图像，src 代码要裁剪的图像  0 0 dw dh 目标图像尺寸
        //x,y,x+dw,y+dh 被裁剪的图像的位置尺寸，null父依赖对象swing，
        g.drawImage(src, 0, 0, dw, dh, x, y, x + dw, y + dh, null);
        dst = rotateImage(dst, 90);
        //释放占用的内存
        g.dispose();
        //输出dst缓存图像到文件中,jpg是格式 gif png jpg
        ImageIO.write(dst, "jpg", new File("c:/s01.jpg"));

        System.out.println(sw);
        System.out.println(sh);

    }

    /**
     * 旋转图片为指定角度
     *
     * @param bufferedimage 目标图像
     * @param degree        旋转角度
     * @return
     */
    public static BufferedImage rotateImage(final BufferedImage bufferedimage,
                                            final int degree) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, type))
                .createGraphics()).setRenderingHint(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
        graphics2d.drawImage(bufferedimage, 0, 0, null);
        graphics2d.dispose();
        return img;
    }

    /**
     * 变更图像为指定大小
     *
     * @param bufferedimage 目标图像
     * @param w             宽
     * @param h             高
     * @return
     */
    public static BufferedImage resizeImage(final BufferedImage bufferedimage,
                                            final int w, final int h) {
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = create(w, h, type))
                .createGraphics()).setRenderingHint(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage
                .getWidth(), bufferedimage.getHeight(), null);
        graphics2d.dispose();
        return img;
    }

    /**
     * 水平翻转图像
     *
     * @param bufferedimage 目标图像
     * @return
     */
    public static BufferedImage flipImage(final BufferedImage bufferedimage) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = create(w, h, bufferedimage
                .getColorModel().getTransparency())).createGraphics())
                .drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
        graphics2d.dispose();
        return img;
    }

    public static BufferedImage create(int w, int h, int type) {
        return new BufferedImage(w, h, type);
    }
}
